package com.yzyz.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.yzyz.bean.hotel.HotelPictureBean;
import com.yzyz.dao.IHotelPictureDao;

/**
 * 酒店图片的数据库访问类
 * @author david.sun
 * @date 2010-7-11
 */
public class HotelPictureDaoImpl implements IHotelPictureDao {

	private JdbcTemplate jdbcTemplate;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<HotelPictureBean> getAll(int hotelId) {
		
		return this.jdbcTemplate.query("select * from hotel_picture where hotelId=" + hotelId, new HotelPictureRowMapper());
	}

	@Override
	public int delete(int id) {
	
		return this.jdbcTemplate.update("delete from hotel_picture where id=" + id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object get(int id) {
		List<HotelPictureBean> list = this.jdbcTemplate.query("select * from hotel_picture where id=" + id, new HotelPictureRowMapper());
		if(list == null || list.size() != 1) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public int getCount(int ownerId) {
		
		return this.jdbcTemplate.queryForInt("select count(*) from hotel_picture where hotelId=" + ownerId);
	}

	@Override
	public int save(Object object) {
		final HotelPictureBean p = (HotelPictureBean)object;
		final String sql = "insert into hotel_picture(name, description, contents, hotelId) values(?,?,?,?)";
	
		KeyHolder key = new GeneratedKeyHolder();
		this.jdbcTemplate.update(new PreparedStatementCreator(){

			@Override
			public PreparedStatement createPreparedStatement(Connection conn)
					throws SQLException {
				PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, p.getName());
				ps.setString(2, p.getDescription());
				ps.setBytes(3, p.getContents());
				ps.setInt(4, p.getHotelId());
				return ps;
			}}, key);
		p.setId(key.getKey().intValue());
		return key.getKey().intValue();
	}

	@Override
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;

	}

	@Override
	public void update(Object object) {
		HotelPictureBean hotel = (HotelPictureBean)object;
		String sql = "update hotel_picture set name=?, description=? where id=?";
		this.jdbcTemplate.update(sql, new Object[]{hotel.getName(), hotel.getDescription(), hotel.getId()});

	}

	@Override
	public int updateAvailable(int id, boolean available) {
		return 0;
	}
	
	private class HotelPictureRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int index) throws SQLException {
			HotelPictureBean p = new HotelPictureBean();
			p.setContents(rs.getBytes("contents"));
			p.setName(rs.getString("name"));
			p.setDescription(rs.getString("description"));
			p.setId(rs.getInt("id"));
			p.setHotelId(rs.getInt("hotelId"));
			return p;
		}
		
	}

}
